package com.lxr.activity;


	import java.io.File;  
    import java.io.FileNotFoundException;
	import java.io.FileOutputStream;  
	import java.io.IOException;  
	import android.content.Intent;
	import android.content.pm.ActivityInfo;  
	import android.graphics.PixelFormat;  
	import android.hardware.Camera;  
	import android.os.AsyncTask;  
	import android.os.Bundle;  
	import android.util.Log;  
	import android.view.KeyEvent;  
	import android.view.SurfaceHolder;  
	import android.view.SurfaceView;  
	import android.view.View;  
	import android.view.Window;  
	import android.view.View.OnClickListener;  
	import android.widget.Button;  
	import android.app.Activity;
	import com.wxq.draw.DrawConstants;
	import com.wxq.draw.DrawDBTool;
	import java.util.Calendar;
import com.wxq.schoolmap.R;


	public class CameraActivity extends Activity {
	  
		private final static String TAG = "CameraActivity";  
	    private SurfaceView surfaceView;  
	    private SurfaceHolder surfaceHolder;  
	    private Camera camera;  
	    private File picture;  
	    private Button btnSave,btnback;  
	    private String ID;
	    private DrawDBTool dbTool;

	    
	       
	    @Override  
	    public void onCreate(Bundle savedInstanceState) {  
	        super.onCreate(savedInstanceState);  
	        Log.i(TAG,"activity created!!!!!!!");
	        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);  
	        this.requestWindowFeature(Window.FEATURE_NO_TITLE);  
	        setContentView(R.layout.camera);  
	        
	        
	        Intent intent=getIntent();
	        Bundle bundle=intent.getExtras();
	        ID=bundle.getString("spotId2");
	       
	        Log.i(TAG,ID+"lllllllllllllll");
	       
	       
	        setupViews();  
	    }  
	      


	    
	    @SuppressWarnings("deprecation")
		private void setupViews(){  
	        surfaceView = (SurfaceView) findViewById(R.id.camera_preview); // Camera interface to instantiate components   
	        surfaceHolder = surfaceView.getHolder(); // Camera interface to instantiate components   
	        surfaceHolder.addCallback(surfaceCallback); // Add a callback for the SurfaceHolder   
	        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);  
	        
	        dbTool = new DrawDBTool(this);
	        dbTool.open();
	        
	        btnSave = (Button) findViewById(R.id.save_pic);
	        btnback = (Button) findViewById(R.id.back);
	        
	        
	        btnSave.setOnClickListener(new OnClickListener() {  
	            public void onClick(View v) {  
	                takePic();
	                //showlist();
	                }  
	        });  
	        btnback.setOnClickListener(new OnClickListener() {  
	            public void onClick(View v) { 
	            	 
	            	finish();    
	            }  
	        });    

	  }
	       
	


	    
	    @Override  
	    public boolean onKeyDown(int keyCode, KeyEvent event) {  
	        if (keyCode == KeyEvent.KEYCODE_CAMERA  
	                || keyCode == KeyEvent.KEYCODE_SEARCH) {  
	            takePic();  
	            return true;  
	        }  
	        return super.onKeyDown(keyCode, event);  
	    }  
	  
	    private void takePic() {  

	        camera.takePicture(null, null, pictureCallback); // picture 
	        //camera.stopPreview();// stop the preview
	       // showlist();
	    }  
	  
	    // Photo call back   
	    Camera.PictureCallback pictureCallback = new Camera.PictureCallback() {  
	        //@Override   
	        public void onPictureTaken(byte[] data, Camera camera) {  
	            new SavePictureTask().execute(data);  
	            camera.startPreview();  
	            
	        }  
	    };  
	  
	    // save pic   
	    class SavePictureTask extends AsyncTask<byte[], String, String> {  
	        @Override  
	        protected String doInBackground(byte[]... params) {  
	            String fname = Calendar.getInstance().getTime().getTime()+".jpg";
	            Log.i(TAG,android.os.Environment.getExternalStorageDirectory()+DrawConstants.ImagesFolder ); 
	            File file = new File(android.os.Environment.getExternalStorageDirectory()+DrawConstants.ImagesFolder);
	            if (!file.exists()) {file.mkdir();}
	            picture = new File(file,fname);// create file   
	            try {
	            	picture.createNewFile();
	                FileOutputStream fos = new FileOutputStream(picture);
	                fos.write(params[0]);
	                fos.flush();
	                fos.close();
	                Log.i(TAG,"save!!");
	                } catch (FileNotFoundException e) {
	                 e.printStackTrace();
	                } catch (IOException e) {
	                 e.printStackTrace();
	                }
	            dbTool.addimage(ID,fname+";");
	            return null;  
	        } 
	    }  
	  
	    // SurfaceHodler Callback handle to open the camera, off camera and photo size changes   
	    SurfaceHolder.Callback surfaceCallback = new SurfaceHolder.Callback() {  
	  
	        public void surfaceCreated(SurfaceHolder holder) {  
	            Log.i(TAG, "surfaceCallback====");  
	            camera = Camera.open(); // Turn on the camera   
	            try {  
	                camera.setPreviewDisplay(holder); // Set Preview   
	            } catch (IOException e) {  
	                camera.release();// release camera   
	                camera = null;  
	            }  
	        }  
	  
	        public void surfaceChanged(SurfaceHolder holder, int format, int width,  
	                int height) {  
	            Log.i(TAG,"====surfaceChanged");  
	            Camera.Parameters parameters = camera.getParameters(); // Camera parameters to obtain   
	            parameters.setPictureFormat(PixelFormat.JPEG);// Setting Picture Format   
	            parameters.set("rotation", 180); // Arbitrary rotation   
	            camera.setDisplayOrientation(90);  
	            //parameters.setPreviewSize(400, 300); // Set Photo Size   
	            camera.setParameters(parameters); // Setting camera parameters   
	            camera.startPreview(); // Start Preview   
	        }  
	  
	        public void surfaceDestroyed(SurfaceHolder holder) {  
	            Log.i(TAG,"====surfaceDestroyed");  
	            camera.stopPreview();// stop preview   
	            camera.release(); // Release camera resources   
	            camera = null;  
	        }  
	    };  
	    
	    @Override
		protected void onDestroy() {
			dbTool.close();
			super.onDestroy();
		}	 
	}  